<?php
class Deals extends MY_Controller {

	var $currentPage = "deals";
	var $caption = "Deal";	
	
	public function index() {
		
		//$data['dealList'] = $this->mDeal->getAllEntries();
		
	    $join_tables = array($this->mItem );
		
		$data['dealList'] = $this->mDeal->getAllEntriesWithJoin($join_tables);
		
		$this->loadAdminPage($data );
	}

	public function create($data='') {
		$this->action = 'add';
		
		$join_tables = array($this->mBusinessnature);
		$data['tenantlist'] = $this->mTenant->getAllEntriesWithJoin($join_tables);
		
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		
		$data['itemlist'] = $this->mItem->getAllEntries();
		
		$this->loadAdminPage($data);
	}
	
	public function add() {
		$deal = &$this->mDeal;
		
		$this->setValues($deal);
		
		$this->message = INSERT_ERROR;
		
		$this->message = INSERT_SUCCESS;
		$deal->insert();
			
		
		$this->index();
	}
	
	public function edit($id) {
		$deal = $this->mDeal->getEntryById($id);
		
		$data= array();
		
		
		$data['itemlist'] = $this->mItem->getAllEntries();
		
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		
		$join_tables = array($this->mBusinessnature);
		$data['tenantlist'] = $this->mTenant->getAllEntriesWithJoin($join_tables);
		
			
		if(count($deal) == 0) {
			show_error("Invalid $caption", 500, "Invalid data");
		}
	
		$data['dealList'] = $this->mDeal->getAllEntries();
	
		$data['deal'] = $deal[0];
	
		$this->action = 'edit';
		$this->id = $deal[0]->id;
		$this->loadAdminPage($data);		
	}
	
	public function update() {
		
		
		$deal = &$this->mDeal;
		
		$this->setValues($deal);
		
		//echo $deal->content;
		
		$this->message = UPDATE_ERROR;
	
		if(strlen($deal->id) != 0) {
		
			if($deal->update())
				$this->message = UPDATE_SUCCESS;
			
		}
		
		$this->edit($deal->id);
	}
	
	public function delete($id) {
		$this->message = DELETE_ERROR;
		
		if($this->mDeal->delete($id))
			$this->message = DELETE_SUCCESS;
		
		$this->index();		
	}
	
	function setValues($deal){
		$deal->id = $this->input->post('id',TRUE);
		$deal->tenant_id = $this->input->post('tenant_id',TRUE);
		//ECHO "--------------------------------------".$this->input->post('item_id',TRUE);
		
		if($deal->id == 0)
			$deal->dealtype = 'T'; 
			
		if(($this->input->post('dealType',TRUE)) != null)
			$deal->dealtype = $this->input->post('dealType',TRUE);
			
			
		if(($this->input->post('ishotdeal',TRUE)) != null)
			$deal->ishotdeal = $this->input->post('ishotdeal',TRUE);
		
			
		$deal->item_id = $this->input->post('item_id',TRUE);
		
		//if($deal->item_id != null)
		//	$this->mItem->getTenantOfItem();
		
		$deal->deal_end_date = $this->formatDate($this->input->post('deal_end_date' , TRUE));
		$deal->deal_category_id = $this->input->post('deal_category_id',TRUE);
		$deal->discount = $this->input->post('discount',TRUE);
		$deal->price = $this->input->post('price',TRUE);
		$deal->item_quantity = $this->input->post('item_quantity',TRUE);
		//$deal->status = $this->input->post('status',TRUE);
		$deal->description = $this->input->post('description',TRUE);
		
	
	}
}
?>